package com.xrtn.job;

import com.xrtn.bean.InitGradeDoctor;
import com.xrtn.bean.SysUserInfo;
import com.xrtn.bean.TDoctorMonthRel;
import com.xrtn.bean.TUserNersingRel;
import com.xrtn.mapper.*;
import com.xrtn.utils.DateUtils;
import com.xrtn.utils.SendValidataCode;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;

import java.math.BigDecimal;
import java.util.*;
import java.util.stream.Collectors;

/**
 * Created by tu_pei on 2018/3/19.
 * 每月月末核算本月 订单数、 满意度 、 月治愈率 核算本月业绩等级
 */
public class DoctorMonthSaleJob {

    @Autowired
    private SysUserInfoMapper sysUserInfoMapper;

    @Autowired
    private TOrderInfoMapper orderInfoMapper;

    @Autowired
    private TReplyInfoMapper replyInfoMapper;

    @Autowired
    private TUserTreatmentRelMapper treatmentRelMapper;
    @Autowired
    private TDoctorMonthRelMapper doctorMonthRelMapper;

    @Autowired
    private InitGradeDoctorMapper gradeDoctorMapper;

    @Autowired
    private AccountDoctorInfoMapper accountDoctorInfoMapper;

    public void work() {

        Map<String,Object> params = new HashMap<>();
        params.put("userStatus", 0);// 0 在职 ， 1 离职 2辞退

        List<SysUserInfo> userList = sysUserInfoMapper.selectUserListByMap(params);

        Integer orderCount = 0, replyRatio = 0;

        BigDecimal treatmentRaion = new BigDecimal(0);

        for (SysUserInfo userInfo : userList){
            //最高等级为 5级
            Integer gradeNumber = userInfo.getUserLevel();
            if(userInfo.getUserLevel() < 5 ){
                gradeNumber = userInfo.getUserLevel()+ 1;
            }
            params.put("gradeNumber", gradeNumber );
            InitGradeDoctor gradeDoctor = gradeDoctorMapper.selectByMap(params);

            TDoctorMonthRel monthRel = new TDoctorMonthRel();

            //当月订单数
            orderCount = orderInfoMapper.selectDoctorOrderCount(userInfo.getUserId());
            //月满意度比率
            replyRatio = replyInfoMapper.selectUserReplyRatio(userInfo.getUserId());

            //月治愈率
            treatmentRaion = treatmentRelMapper.selectTreatmentMonth(userInfo.getUserId());

            monthRel.setMonthStr(DateUtils.format(new Date(),"yyyy-MM"));
            //治愈率
            monthRel.setMonthTreatmentRatio(treatmentRaion);
            //医生
            monthRel.setDoctorId(userInfo.getUserId());
            //订单数
            monthRel.setOrderNumber(orderCount);
            //满意度
            monthRel.setReplyRatio(new BigDecimal(replyRatio));
            //投诉率
            monthRel.setComplainRatio(new BigDecimal(0));

            monthRel.setCreateTime(new Date());

            System.out.println("monthRel = " + JSONObject.fromObject(monthRel).toString());

            monthRel.setUserLevel(userInfo.getUserLevel());

            if( monthRel.getOrderNumber() >= gradeDoctor.getGradeStartScore() &&
                    monthRel.getReplyRatio().intValue() >= gradeDoctor.getReplyRatio() &&
                    monthRel.getMonthTreatmentRatio().intValue() >= gradeDoctor.getMonthTreatmentRatio()){
                if(userInfo.getUserLevel() < 5 ){
                    //本月达到上一级别标准
                    monthRel.setUserLevel(userInfo.getUserLevel() + 1);
                }
            }
            doctorMonthRelMapper.insertSelective(monthRel);

            //获取用户提成总数
            BigDecimal saleCost = accountDoctorInfoMapper.selectDoctorSumCost(userInfo.getUserId());
            //发送短信 提成统计情况推送到技师端

            SendValidataCode.sendSysUserMonth(userInfo.getUserPhone(),String.valueOf(orderCount),String.valueOf(treatmentRaion),String.valueOf(saleCost),"248110");

        }

    }
}
